<template>
	<view>
		<u-navbar title="登录" bgColor="transparent" :autoBack="true"></u-navbar>
		<view class="bgc"></view>
		<view class="box">
			<view class="logo"><image :src="imgUrl + 'logo.png'" mode="aspectFill"></image></view>
			<view class="btn">
				<u-button
					text="微信授权用户信息"
					v-if="show"
					@click="wxLogin"
					shape="circle"
					:customStyle="btnStyle"
				/>
				<u-button
					text="获取用户手机号"
					v-else
					openType="getPhoneNumber"
					@getphonenumber="getMobile"
					shape="circle"
					:customStyle="btnStyle"
				/>
			</view>
		</view>
	</view>
</template>

<script>
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex';
export default {
	data() {
		return {
			imgUrl: this.$IMAGE_URL,
			btnStyle: {
				background: '#50AA37',
				border: 'none',
				height: '90rpx',
				color: '#fff'
			},
			show: true,
			userinfo: {},
			identity: '',
			type:''//用于判断 是否点菜页面进入
		};
	},
onLoad(options) {
	console.log(options);
	this.type=options.type
},
	computed: {
		...mapGetters(['userInfo'])
	},

	methods: {
		...mapActions(['getUserInfo']),
		wxLogin() {
			let that = this;
			uni.showLoading({
				title: '加载中...',
				mask: true
			});
			uni.login({
				provider: 'weixin',
				success(res) {
					uni.setStorageSync('code', res.code);

					that.$http
						.post('wanlshop/user/third', {
							platform: 'mp_weixin',
							loginData: res.code
						})
						.then(res => {
							uni.hideLoading();
							if (res.code == 1) {
								that.userinfo = res.data.user.userinfo;
								console.log(res.data.user.userinfo, 'res.data.user.userinfo');
								that.identity = res.data.identity;
								if (!that.userinfo.mobile) {
									that.show = false;
								} else {
									that.getUserInfo(that.userinfo.token);
									if (!that.identity) {
										uni.reLaunch({
											url: '/pagesA/my/autonym'
										});
									} else {
										uni.$emit('getMember');
										uni.navigateBack();
									}
								}
							}
						});
				}
			});
		},
		getMobile(event) {
			uni.showLoading({
				title: '绑定中...',
				mask: true
			});
			this.$http
				.post('wanlshop/user/bindPhone', {
					encryptedData: event.detail.encryptedData,
					iv: event.detail.iv,
					user_id: this.userinfo.id
				})
				.then(res => {
					uni.hideLoading();
					if (res.code == 1) {
						this.getUserInfo(this.userinfo.token);
						if (!this.identity) {
							uni.reLaunch({
								url: '/pagesA/my/autonym'
							});
						} else {
							uni.$emit('getMember');
							uni.navigateBack();
						}
					}
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.bgc {
	background-repeat: no-repeat;
	background-size: 100% 100%;
	box-sizing: border-box;
	height: 240rpx;
	background-image: url();
}

.box {
	position: fixed;
	left: 50%;
	transform: translateX(-50%);
	top: 30%;
	padding: 0 60rpx;
	width: 100%;
	box-sizing: border-box;

	.logo {
		width: 250rpx;
		height: 250rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 0 auto;
		border-radius: 50%;
		background-color: #ffc100;
		overflow: hidden;
		image {
			// width: 170rpx;
			// height: 170rpx;
			width: 100%;
			height: 100%;
		}
	}

	.btn {
		margin-top: 270rpx;
	}
}
</style>
